﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI;
using System.Reflection;

using Developmentalmadness.Web.UI.AjaxAbstractions;

namespace Developmentalmadness.Web.UI.ExtJsControlLibrary
{
    public class ExtJsMenuBaseItem : ExtJsComponent, IMenuItem
    {
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.menu.BaseItem";

            String[] properties = new String[]{
                "activeClass", "canActivate", "handler", 
                "hideDelay", "hideOnClick", "scope"
            };

            Type component = typeof(ExtJsMenuBaseItem);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'Ext.menu.BaseItem.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            return list;
        }

        public String activeClass { get; set; }

        public Boolean? canActivate { get; set; }

        public String handler { get; set; }

        public Int32? hideDelay { get; set; }

        public Boolean? hideOnClick { get; set; }

        public String scope { get; set; }

        #region IMenuItem Members

        public virtual string GetMenuItemScript()
        {
            ScriptControlDescriptorBase descriptor = (ScriptControlDescriptorBase)GetScriptDescriptors()[0];

            return descriptor.WriteScript(ScriptType.NewOnly);
        }

        #endregion
    }
}
